package ast;

import mem.interfaces.IMem;
import environnement.interfaces.IEnv;
import ast.interfaces.IAST;
import ast.interfaces.IASTfun;
import ast.interfaces.IASTidents;

public class ASTfun implements IASTfun{
	private String nom;
	private IAST idents;
	private IAST exp;
	
	public ASTfun(String nom,IAST idents,IAST prog){
		this.nom = nom;
		this.idents = idents;
		this.exp = prog;
	}

	@Override
	public Object eval(IAST ast, IEnv e, IMem m) {
		ASTidents ast_idents = (ASTidents) getIdents();
		
		//Ajout des variables dans l'environnement
		for(String var : ast_idents.getIdents()){
			e.getR().put(var, m.getCurrent_id());
			m.increment();
		}
		
		//Ajout de la fontion
		e.getF().put(getNom(), getExp());
		
		return null;
	}

	@Override
	public String toTreeString(String ident, IEnv e, IMem m) {
		return "FUN " + getNom() + " " + getIdents().toTreeString(ident, e, m)+ " = " + getExp().toTreeString(ident, e, m);
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public IAST getIdents() {
		return idents;
	}

	public void setIdents(IAST idents) {
		this.idents = idents;
	}

	public IAST getExp() {
		return exp;
	}

	public void setExp(IAST exp) {
		this.exp = exp;
	}
}
